<template>
	<view>
		<view class="header">
			<view class="header_main">
				<view class="seach_itme">
					<view class="gui-icons">&#xe604;</view>
					<input type="text" v-model="seachVal" class="iconfont seach_input" @input="inputting"
						@confirm="showArticle" hold-keyboard placeholder="请输入关键字" adjust-position="false" />
				</view>
				<!-- <view @tap="cancelFun" class="cancel_btn">
					取消
				</view> -->
			</view>
			<view class="mask" v-if="isShowMask">
				<view class="seach_list">
					<view class="list_item uni-ellipsis qoverDot" @tap="historySeach(item)"
						v-for="(item, index) in seachList" :key="index">
						{{item.title}}
					</view>
				</view>
				<!-- <gui-empty v-if="seachList.length <= 0">
					<view slot="img" class="gui-flex gui-rows gui-justify-content-center">
						<image class="gui-empty-img"
							src="@/static/noImg.png">
						</image>
					</view>
					<text slot="text" class="gui-text-small gui-block-text gui-text-center gui-margin-top"
						style="color:#9DABFF;">暂无数据 ......</text>
				</gui-empty> -->
			</view>
		</view>
		<view class="history">
			<view class="history_lable">
				<view class="history_text c333">
					历史搜索
				</view>
				<view class="history_del">
					<view class="gui-icons" @tap="deteleHis">&#xe794;</view>
				</view>
			</view>
			<view class="history_main mt20">
				<view class="history_item" @tap="historySeach2(item)" v-for="(item, index) in historyList" :key="index">
					{{item}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var _self;
	export default {
		data() {
			return {
				isShowMask: false,
				seachType: '',
				seachVal: '',
				historyList: [],
				seachList: []
			}
		},
		onLoad(e) {
			console.log(e);
			_self = this;
			this.seachType = e.type;
			this.getHistory();
		},
		methods: {
			inputting(e) {
				this.seachVal = e.detail.value;
			},
			clear() {
				console.log(e);
				this.seachVal = '';
			},
			deteleHis() {
				uni.showModal({
					title: '清空',
					content: '清空历史记录？',
					success: (res) => {
						if (res.confirm) {
							this.historyList = [];
							uni.removeStorageSync('historyList');

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			/**
			 * 点击历史记录搜索
			 */
			historySeach(item) {
				let _this = this;
				uni.navigateTo({
					url: item.url
				})
			},
			/**
			 * 点击历史记录搜索
			 */
			historySeach2(item) {
				let _this = this;
				this.seachVal = item;
				this.showArticle();
			},
			/**
			 * 搜索
			 */
			async showArticle() {
				let _this = this;
				if (this.seachVal == "") {
					uni.showToast({
						title: '您没有输入任何内容',
						icon: 'none'
					});
					return;
				}
				console.log(this.seachType);
				if (this.seachType == 1) { // 政策解读
					uni.navigateTo({
						url: `/pages/index/policy/search?searchVal=${this.seachVal}`
					})
				}else if (this.seachType == 2) { // 线上课件
					uni.navigateTo({
						url: `/pages/index/training-search?searchVal=${this.seachVal}`
					})
				}
				this.history();
			},
			/**
			 * 获取首页历史记录
			 */
			getHistory() {
				let _self = this;
				uni.getStorage({
					key: 'historyList',
					success: function(res) {
						_self.historyList = res.data;
					},
					fail(err) {
						_self.historyList = [];
					}
				});
			},
			/**
			 * 存储历史记录
			 */
			history() {
				let _self = this;
				let isPush = true;
				this.historyList.some((v) => {
					if (v === this.seachVal) {
						isPush = false;
						return;
					}
				})
				if (isPush) {
					this.historyList.unshift(this.seachVal);
				}
				let historyValList = [];
				for (var i = 0; i < 10; i++) {
					if (this.historyList[i]) {
						historyValList.push(this.historyList[i]);
					}
				}
				uni.setStorage({
					key: 'historyList',
					data: historyValList,
					success: function() {
						console.log('success');
					}
				});
			},
			/**
			 * 返回上一页
			 */
			cancelFun() {
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.header {
		width: 100%;
		display: flex;
		flex-direction: column;
		position: relative;

		.mask {
			min-height: 800rpx;
			max-height: 900rpx;
			overflow: auto;
			width: 100%;
			background-color: #fff;
			position: absolute;
			top: 100rpx;
			padding: 20rpx;
			box-sizing: border-box;
			font-size: 28rpx;

			.seach_list {
				.list_item {
					color: #999999;
					height: 70rpx;
					line-height: 70rpx;
					border-bottom: 1px solid #EEEEEE;
				}
			}

		}

		.header_main {
			width: 100%;
			height: 100rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			padding: 0 25rpx;
			box-sizing: border-box;

			.seach_itme {
				width: 100%;
				position: relative;
				display: flex;
				flex-direction: row;
				align-items: center;

				.gui-icons {
					position: absolute;
					left: 15rpx;
					color: #999999;
				}

				.seach_input {
					width: 100%;
					height: 71rpx;
					background-color: #EAEAEA;
					border-radius: 36px;
					padding-left: 80rpx;
				}
			}

			.cancel_btn {
				color: #999999;
				font-size: 30rpx;
				width: 15%;
				text-align: center;
			}
		}

	}

	.history {
		display: flex;
		flex-direction: column;
		padding: 20rpx;
		box-sizing: border-box;

		.history_lable {
			color: #999999;
			font-size: 30rpx;
			height: 50rpx;
			line-height: 50rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.history_main {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;

			.history_item {
				width: 50%;
				height: 50rpx;
				line-height: 50rpx;
				font-size: 30rpx;
				color: #808080;
			}
		}
	}
</style>